import React from "react";
import {Form, Input, InputNumber} from "antd";
import {SubjectPo} from "./index";

/**
 * 编辑单元格
 */
export interface EditableCellProps extends React.HTMLAttributes<HTMLElement> {
  editing: boolean;
  dataIndex: string;
  title: any;
  inputType: 'number' | 'text';
  record: SubjectPo;
  index: number;
  children: React.ReactNode;
}

export const EditableCell: React.FC<EditableCellProps> = ({
                                                            editing,
                                                            dataIndex,
                                                            title,
                                                            inputType,
                                                            record,
                                                            index,
                                                            children,
                                                            ...restProps
                                                          }) => {
  const inputNode = inputType === 'number' ? <InputNumber/> : <Input/>;

  return (
    <td {...restProps}>
      {editing ? (
        <Form.Item
          name={dataIndex}
          style={{margin: 0}}
          rules={[
            {
              required: true,
              message: `请输入 ${title}!`,
            },
          ]}
        >
          {inputNode}
        </Form.Item>
      ) : (
        children
      )}
    </td>
  );
};
